﻿Public Class ReportGenerator
    Inherits System.Web.UI.Page

    Private Sub ReportGenerator_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Dim IdReport As EnumTipusReport?

        If Me.IsPostBack Then Return

        IdReport = Request("Report")
        If Not IdReport.HasValue Then
            mostrarError(My.Resources.StringLibrary.InformeObligatori)
            Return
        End If

        Select Case IdReport
            Case EnumTipusReport.ControlDeCaixa
                Dim IdHostal As Integer? = Request("IdHostal")
                Dim IdUsuari As Integer? = Request("IdUsuari")
                Dim DataInicial As Date?
                Dim DataFinal As Date?
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then DataInicial = New Date(Long.Parse(Request("DataInici")))
                If Request("DataFi") IsNot Nothing Then DataFinal = New Date(Long.Parse(Request("DataFi")))

                If IdHostal Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                End If

                If IdUsuari Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.UsuariObligatori)
                    Return
                End If

                Dim LListaHostals As List(Of Hostal)
                Dim LListaUsuaris As List(Of Usuari)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal Or IdHostal = -1).ToList()
                    LListaUsuaris = (From U As Usuari In Entities.Usuaris Where U.Id = IdUsuari Or IdUsuari = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=ControlCajaHostales.xlsx")

                FileName = ExcelControlDeCaixaHostals.Generar(LListaHostals, LListaUsuaris, DataInicial, DataFinal)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.EstatHostal
                Dim IdHostal As Integer? = Request("IdHostal")
                Dim IdUsuari As Integer? = Request("IdUsuari")
                Dim DataInicial As Date?
                Dim DataFinal As Date?
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then DataInicial = New Date(Long.Parse(Request("DataInici")))
                If Request("DataFi") IsNot Nothing Then DataFinal = New Date(Long.Parse(Request("DataFi")))

                If IdHostal Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                End If

                If IdUsuari Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.UsuariObligatori)
                    Return
                End If

                Dim LListaHostals As List(Of Hostal)
                Dim LListaUsuaris As List(Of Usuari)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal Or IdHostal = -1).ToList()
                    LListaUsuaris = (From U As Usuari In Entities.Usuaris Where U.Id = IdUsuari Or IdUsuari = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=EstadoHostales.xlsx")

                FileName = ExcelEstatHostals.Generar(LListaHostals, LListaUsuaris, DataInicial, DataFinal)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.ResumDiari
                Dim IdHostal As Integer? = Request("IdHostal")
                Dim DataInicial As Date?
                Dim DataFinal As Date?
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then DataInicial = New Date(Long.Parse(Request("DataInici")))
                If Request("DataFi") IsNot Nothing Then DataFinal = New Date(Long.Parse(Request("DataFi")))

                If IdHostal Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                End If

                Dim LListaHostals As List(Of Hostal)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal Or IdHostal = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=ResumenDiario.xlsx")

                FileName = ExcelResumDiari.Generar(LListaHostals, DataInicial, DataFinal)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.PlantillaHoraris
                Dim IdHostal As Integer
                Dim DataInicial As Date
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then
                    DataInicial = New Date(Long.Parse(Request("DataInici")))
                Else
                    mostrarError(My.Resources.StringLibrary.DataObligatoria)
                    Return
                End If

                If Request("IdHostal") Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                Else
                    IdHostal = Request("IdHostal")
                End If

                Dim Hostal As Hostal
                Using Entities As LullabyEntities = New LullabyEntities
                    Hostal = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal).FirstOrDefault
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=PlantillaHorario.xlsx")

                FileName = ExcelPlantillaHoraris.Generar(Hostal, DataInicial)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.DetallGastosIngressos
                Dim IdHostal As Integer? = Request("IdHostal")
                Dim DataInicial As Date?
                Dim DataFinal As Date?
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then DataInicial = New Date(Long.Parse(Request("DataInici")))
                If Request("DataFi") IsNot Nothing Then DataFinal = New Date(Long.Parse(Request("DataFi")))

                If IdHostal Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                End If

                Dim LListaHostals As List(Of Hostal)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal Or IdHostal = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=DetalleGastosIngresos.xlsx")

                FileName = ExcelDetallGastosIngressos.Generar(LListaHostals, DataInicial, DataFinal)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.DetallReserves
                Dim IdHostal As Integer? = Request("IdHostal")
                Dim IdUsuari As Integer? = Request("IdUsuari")
                Dim DataInicial As Date?
                Dim DataFinal As Date?
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then DataInicial = New Date(Long.Parse(Request("DataInici")))
                If Request("DataFi") IsNot Nothing Then DataFinal = New Date(Long.Parse(Request("DataFi")))

                If IdHostal Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                End If

                If IdUsuari Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.UsuariObligatori)
                    Return
                End If

                Dim LListaHostals As List(Of Hostal)
                Dim LListaUsuaris As List(Of Usuari)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal Or IdHostal = -1).ToList()
                    LListaUsuaris = (From U As Usuari In Entities.Usuaris Where U.Id = IdUsuari Or IdUsuari = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=DetalleReservas.xlsx")

                FileName = ExcelDetallReserves.Generar(LListaHostals, LListaUsuaris, DataInicial, DataFinal)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.HoresTreballades
                Dim IdUsuari As Integer? = Request("IdUsuari")
                Dim DataInicial As Date?
                Dim DataFinal As Date?
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then DataInicial = New Date(Long.Parse(Request("DataInici")))
                If Request("DataFi") IsNot Nothing Then DataFinal = New Date(Long.Parse(Request("DataFi")))

                If IdUsuari Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.UsuariObligatori)
                    Return
                End If

                Dim LListaUsuaris As List(Of Usuari)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaUsuaris = (From U As Usuari In Entities.Usuaris Where U.Id = IdUsuari Or IdUsuari = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=HorasTrabajadas.xlsx")

                FileName = ExcelHoresTreballades.Generar(LListaUsuaris, DataInicial, DataFinal)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.Neteges
                Dim IdHostal As Integer? = Request("IdHostal")
                Dim DataInicial As Date?
                Dim DataFinal As Date?
                Dim FileName As String

                If Request("DataInici") IsNot Nothing Then DataInicial = New Date(Long.Parse(Request("DataInici")))
                If Request("DataFi") IsNot Nothing Then DataFinal = New Date(Long.Parse(Request("DataFi")))

                If IdHostal Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                End If

                Dim LListaHostals As List(Of Hostal)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal Or IdHostal = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=TurnosLimpieza.xlsx")

                FileName = ExcelNeteges.Generar(LListaHostals, DataInicial, DataFinal)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case EnumTipusReport.ThingsToDo
                Dim IdHostal As Integer? = Request("IdHostal")
                Dim IdUsuari As Integer? = Request("IdUsuari")
                Dim IdCategoria As Integer? = Request("IdCategoria")
                Dim IdUrgencia As EnumUrgencia? = Request("IdUrgencia")
                Dim IdEstat As Integer? = Request("IdEstat")
                Dim FileName As String

                If IdHostal Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.HostalObligatori)
                    Return
                End If

                If IdUsuari Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.UsuariObligatori)
                    Return
                End If

                If IdCategoria Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.CategoriaObligatoria)
                    Return
                End If

                If IdUrgencia Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.UrgenciaObligatoria)
                    Return
                End If

                If IdEstat Is Nothing Then
                    mostrarError(My.Resources.StringLibrary.EstatObligatori)
                    Return
                End If

                Dim LListaHostals As List(Of Hostal)
                Dim LListaUsuaris As List(Of Usuari)
                Dim LListaCategories As List(Of ThingsToDoCategory)

                Using Entities As LullabyEntities = New LullabyEntities
                    LListaHostals = (From H As Hostal In Entities.Hostals Where H.Id = IdHostal Or IdHostal = -1).ToList()
                    LListaUsuaris = (From U As Usuari In Entities.Usuaris Where U.Id = IdUsuari Or IdUsuari = -1).ToList()
                    LListaCategories = (From C As ThingsToDoCategory In Entities.ThingsToDoCategories Where C.Id = IdCategoria OrElse IdCategoria = -1).ToList()
                End Using

                Response.Clear()
                Response.ContentType = "application/xlsx"
                Response.AddHeader("content-disposition", "attachment; filename=ThingToDo.xlsx")

                FileName = ExcelThingsToDo.Generar(LListaHostals, LListaUsuaris, LListaCategories, IdUrgencia, IdEstat)

                Response.WriteFile(FileName)
                Response.Flush()
                Response.End()

                IO.File.Delete(FileName)

            Case Else
                mostrarError(My.Resources.StringLibrary.ReportNoDefinit)
                Return

        End Select

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Private Sub mostrarError(ByVal missatge As String)
        lblError.Text = missatge
        lblError.Visible = True
    End Sub
End Class